{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a4bf2c0-c60b-4667-91a6-f816b281dbf1",
   "metadata": {},
   "source": [
    "查看样本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bcb4eaa0-cd6f-4f19-9da3-4f3816a637f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>工资</th>\n",
       "      <th>满意度</th>\n",
       "      <th>考核得分</th>\n",
       "      <th>工程数量</th>\n",
       "      <th>月工时</th>\n",
       "      <th>工龄</th>\n",
       "      <th>离职</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>低</td>\n",
       "      <td>3.8</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>中</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.86</td>\n",
       "      <td>5</td>\n",
       "      <td>262</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>中</td>\n",
       "      <td>1.1</td>\n",
       "      <td>0.88</td>\n",
       "      <td>7</td>\n",
       "      <td>272</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>低</td>\n",
       "      <td>7.2</td>\n",
       "      <td>0.87</td>\n",
       "      <td>5</td>\n",
       "      <td>223</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>低</td>\n",
       "      <td>3.7</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>159</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>低</td>\n",
       "      <td>4.1</td>\n",
       "      <td>0.50</td>\n",
       "      <td>2</td>\n",
       "      <td>153</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>低</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.77</td>\n",
       "      <td>6</td>\n",
       "      <td>247</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>低</td>\n",
       "      <td>9.2</td>\n",
       "      <td>0.85</td>\n",
       "      <td>5</td>\n",
       "      <td>259</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>低</td>\n",
       "      <td>8.9</td>\n",
       "      <td>1.00</td>\n",
       "      <td>5</td>\n",
       "      <td>224</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>低</td>\n",
       "      <td>4.2</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>142</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  工资  满意度  考核得分  工程数量  月工时  工龄  离职\n",
       "0  低  3.8  0.53     2  157   3   1\n",
       "1  中  8.0  0.86     5  262   6   1\n",
       "2  中  1.1  0.88     7  272   4   1\n",
       "3  低  7.2  0.87     5  223   5   1\n",
       "4  低  3.7  0.52     2  159   3   1\n",
       "5  低  4.1  0.50     2  153   3   1\n",
       "6  低  1.0  0.77     6  247   4   1\n",
       "7  低  9.2  0.85     5  259   5   1\n",
       "8  低  8.9  1.00     5  224   5   1\n",
       "9  低  4.2  0.53     2  142   3   1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel('员工离职预测模型.xlsx')\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f30932c-9ff4-4b3b-a47b-a69a7aee7883",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>满意度</th>\n",
       "      <th>考核得分</th>\n",
       "      <th>工程数量</th>\n",
       "      <th>月工时</th>\n",
       "      <th>工龄</th>\n",
       "      <th>离职</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>15000.000000</td>\n",
       "      <td>15000.000000</td>\n",
       "      <td>15000.000000</td>\n",
       "      <td>15000.000000</td>\n",
       "      <td>15000.000000</td>\n",
       "      <td>15000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>6.128467</td>\n",
       "      <td>0.716101</td>\n",
       "      <td>3.803200</td>\n",
       "      <td>201.047667</td>\n",
       "      <td>3.498267</td>\n",
       "      <td>0.238133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.486276</td>\n",
       "      <td>0.171163</td>\n",
       "      <td>1.232682</td>\n",
       "      <td>49.942505</td>\n",
       "      <td>1.460093</td>\n",
       "      <td>0.425955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.900000</td>\n",
       "      <td>0.360000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>4.400000</td>\n",
       "      <td>0.560000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>156.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>6.400000</td>\n",
       "      <td>0.720000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>200.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>8.200000</td>\n",
       "      <td>0.870000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>245.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>310.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                满意度          考核得分          工程数量           月工时            工龄  \\\n",
       "count  15000.000000  15000.000000  15000.000000  15000.000000  15000.000000   \n",
       "mean       6.128467      0.716101      3.803200    201.047667      3.498267   \n",
       "std        2.486276      0.171163      1.232682     49.942505      1.460093   \n",
       "min        0.900000      0.360000      2.000000     96.000000      2.000000   \n",
       "25%        4.400000      0.560000      3.000000    156.000000      3.000000   \n",
       "50%        6.400000      0.720000      4.000000    200.000000      3.000000   \n",
       "75%        8.200000      0.870000      5.000000    245.000000      4.000000   \n",
       "max       10.000000      1.000000      7.000000    310.000000     10.000000   \n",
       "\n",
       "                 离职  \n",
       "count  15000.000000  \n",
       "mean       0.238133  \n",
       "std        0.425955  \n",
       "min        0.000000  \n",
       "25%        0.000000  \n",
       "50%        0.000000  \n",
       "75%        0.000000  \n",
       "max        1.000000  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "399f152e-15eb-4946-99e2-2b16d27feffd",
   "metadata": {},
   "source": [
    "特征为工资的列为字符类型，特征编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "041bf1c7-fa59-4125-b586-4f5f6129727f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\xueka\\AppData\\Local\\Temp\\ipykernel_22596\\2487739542.py:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})\n"
     ]
    }
   ],
   "source": [
    "df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "155b54ae-f30d-4508-a345-ef847f57a4bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>工资</th>\n",
       "      <th>满意度</th>\n",
       "      <th>考核得分</th>\n",
       "      <th>工程数量</th>\n",
       "      <th>月工时</th>\n",
       "      <th>工龄</th>\n",
       "      <th>离职</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3.8</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.86</td>\n",
       "      <td>5</td>\n",
       "      <td>262</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1.1</td>\n",
       "      <td>0.88</td>\n",
       "      <td>7</td>\n",
       "      <td>272</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>7.2</td>\n",
       "      <td>0.87</td>\n",
       "      <td>5</td>\n",
       "      <td>223</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>3.7</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>159</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   工资  满意度  考核得分  工程数量  月工时  工龄  离职\n",
       "0   0  3.8  0.53     2  157   3   1\n",
       "1   1  8.0  0.86     5  262   6   1\n",
       "2   1  1.1  0.88     7  272   4   1\n",
       "3   0  7.2  0.87     5  223   5   1\n",
       "4   0  3.7  0.52     2  159   3   1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e855e00-6058-41b3-90e0-bc2a55770a92",
   "metadata": {},
   "source": [
    "数据划分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d0fb2c7e-aa4c-4113-8365-a7ba54bfcf97",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop(columns='离职')\n",
    "y = df['离职']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6289317e-62cb-464e-852a-fdd8cdbcb1db",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daa7e686-b106-40f1-98ed-7e60d514cd94",
   "metadata": {},
   "source": [
    "模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "040c9f1b-d1ee-403a-9c95-2155d41c0770",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.98      0.99      0.99      2308\n",
      "           1       0.96      0.95      0.96       692\n",
      "\n",
      "    accuracy                           0.98      3000\n",
      "   macro avg       0.97      0.97      0.97      3000\n",
      "weighted avg       0.98      0.98      0.98      3000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import classification_report\n",
    "model = DecisionTreeClassifier(max_depth=5, random_state=42)\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fcff1422-9cd3-4261-9eb9-5fa5baa9e229",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjEsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvc2/+5QAAAAlwSFlzAAAPYQAAD2EBqD+naQAAJWZJREFUeJzt3X9wVNX9//F3fpCEKAliSgIhGsUqIAg1DPkEZBz6TU0rg/UPpxl1IEWFUnHGkqk/EAEVNdYRilOjGVGK/Y4U1FE+TmFiFWUcSjqMIDPYIg4iJEUTiJYEE8ivvZ85Z/dudsMmEMzek93zfMxsN3tz7+ZyTXNee877nJvgOI4jAAAAhiSa+sEAAAAKYQQAABhFGAEAAEYRRgAAgFGEEQAAYBRhBAAAGEUYAQAARhFGAACAUckSA3w+n3z99dcybNgwSUhIMH06AADgPKh1VU+dOiWjR4+WxMTE2A4jKojk5eWZPg0AAHAB6urqZMyYMbEdRlSPiPuPycjIMH06AADgPDQ3N+vOBLcdj+kw4g7NqCBCGAEAILacq8SCAlYAAGAUYQQAABhFGAEAAEYRRgAAgFGEEQAAYBRhBAAAGEUYAQAARhFGAACAUYQRAAAQW2Hk448/ljlz5uib3qgV1bZs2XLOY3bs2CHXX3+9pKamylVXXSUbNmy40PMFAAC2h5GWlhaZPHmyVFZWntf+X331lcyePVtmzZol+/btk9/97ndyzz33yHvvvXch5wsAAOJMv+9N84tf/EI/zldVVZVcccUVsnr1av16/PjxsnPnTvnjH/8oJSUl/f3xAAAgzkT9Rnk1NTVSXFwctk2FENVD0pu2tjb9CL3rHwAA8cxxHOnyOdLpc6S9yyedXY50dPmkvdOnX6tn9Vo92vTXjnQEvhe6X0fge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HE33HCDvhtsZ2enLFq0SB555BGPztoO9b20heruvqdPn9b1OgMtbntGEBueeeYZXVj5zjvv6AI2DBx12+65c+fqguGsrCzTpxPXfD6f7n16+eWXpaCgQEpLS2XZsmVSVVVl+tTiiiqqVD1OL774ouzdu1fefvtt2bp1q6xatcr0qeEHitueEfXHNykpSRoaGsK2q9c5OTkRj1Hb+7M/Luw6u5577jkdRj744AO57rrronym9l3rL7/8Uo4cOaKr6EMbTSU5OVkOHjwoY8eO9eDM4/93Ws2gGTJkiD7ONX78eP0JUw1HpKSkRP28bbjOy5cv1wH7nnvu0a/VjMeWlhZZuHChDn9qmAc/XG9tYUZGRlR6RZS4/S+n/s+vPqFs37497A+xeq3GdiNR20P3V95///1e98eFXWfl2Wef1Z9mqqurZerUqR6drV3XWk1R379/vx6icR+33HKLzJo1S3+tpkViYH6nZ8yYoYdm3LCnfPHFFzqkEEQG7jqr+rKegcMNgNxmbeAYaQudOJ82pqaBbdiwQU9PWrhwoZ42Vl9fr78/d+5c5+GHHw6b2pucnOw899xzesrpypUrmdobhev8zDPP6Ol8b731lvPNN98EH6dOnTL4r4jPa90Ts2mic51ra2v1jLD77rvPOXjwoPO3v/3NGTlypPPkk08a/FfE33VWf5PVdf7rX/+qp5/+/e9/d8aOHatnQqJ36m+rWkpBPVSzv2bNGv310aNH9ffVNVbXuufU3gceeEC3hWopBqb2/kBqfvRll12mGz81jeyf//xn8Hs33nij/uMc6o033nCuvvpqvb+a2rR161YDZx3f1/nyyy/X/4fo+VB/aDDwv9OhCCPRu867du3SSwGoxlVN833qqaf0tGoM3HXu6OhwHnvsMR1A0tLSnLy8POfee+91/vvf/xo6+9jw0UcfRfyb615b9ayudc9jpkyZov+7qN/nP//5z1E9xwT1P9HrdwEAALC0ZgQAAMQGwggAADCKMAIAAIwijAAAAKMIIwAAwCjCCAAAMIowAgAAjCKMAAAAowgjAADAKMIIAAAwijACAACMIowAAAAx6f8A43Y4JMxfn0QAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_curve\n",
    "import matplotlib.pyplot as plt\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "plt.plot(fpr, tpr)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a757f410-44ea-44d3-a89f-50bcc468f332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.9807007668727021)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_auc_score\n",
    "score = roc_auc_score(y_test, y_pred_proba[:,1])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6dd955de-0493-4d27-ad1f-e0b603d8c669",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>特征名称</th>\n",
       "      <th>特征重要性</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>满意度</td>\n",
       "      <td>0.535920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>工龄</td>\n",
       "      <td>0.155279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>考核得分</td>\n",
       "      <td>0.127614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>工程数量</td>\n",
       "      <td>0.103573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>月工时</td>\n",
       "      <td>0.077614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>工资</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   特征名称     特征重要性\n",
       "1   满意度  0.535920\n",
       "5    工龄  0.155279\n",
       "2  考核得分  0.127614\n",
       "3  工程数量  0.103573\n",
       "4   月工时  0.077614\n",
       "0    工资  0.000000"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features = X.columns\n",
    "importances = model.feature_importances_ \n",
    "\n",
    "importances_df = pd.DataFrame()\n",
    "importances_df['特征名称'] = features\n",
    "importances_df['特征重要性'] = importances\n",
    "importances_df.sort_values('特征重要性', ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "288282c5-3635-43c5-ae6a-5120e12f4de3",
   "metadata": {},
   "source": [
    "进行k折交叉检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a75e7069-5b14-40ab-bca1-e2b389d13780",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.97666667, 0.973     , 0.97133333, 0.97466667, 0.96633333])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "acc = cross_val_score(model, X, y, cv=5)\n",
    "acc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46967c6f-fbe4-44ef-b628-ebe46d1fe1e3",
   "metadata": {},
   "source": [
    "通过网格搜索进行参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ccfc27e6-008a-4692-9e81-bf816aa1b6ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数: {'criterion': 'entropy', 'max_depth': 9, 'min_samples_leaf': 1, 'min_samples_split': 5}\n",
      "最佳交叉验证分数: 0.9795\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.98      1.00      0.99      2308\n",
      "           1       0.99      0.95      0.97       692\n",
      "\n",
      "    accuracy                           0.99      3000\n",
      "   macro avg       0.99      0.97      0.98      3000\n",
      "weighted avg       0.99      0.99      0.99      3000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "param_grid = {\n",
    "    'max_depth': [3, 5, 7, 9, 11], \n",
    "    'min_samples_split': [2, 5, 10],\n",
    "    'min_samples_leaf': [1, 2, 4],\n",
    "    'criterion': ['gini', 'entropy']\n",
    "}\n",
    "\n",
    "grid_search = GridSearchCV(\n",
    "    estimator=DecisionTreeClassifier(random_state=42),\n",
    "    param_grid=param_grid,\n",
    "    cv=5, \n",
    "    scoring='accuracy',\n",
    "    n_jobs=-1\n",
    ")\n",
    "\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "print(\"最佳参数:\", grid_search.best_params_)\n",
    "print(\"最佳交叉验证分数:\", grid_search.best_score_)\n",
    "\n",
    "# 使用最佳参数的模型进行预测\n",
    "best_model = grid_search.best_estimator_\n",
    "y_pred = best_model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18b0aa69-a4e3-4693-9d52-783088e1cd6a",
   "metadata": {},
   "source": [
    "准确率提升至0.99"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7113c21-6f12-48f4-92e4-d84ad2d701df",
   "metadata": {},
   "source": [
    "使用多种模型观察效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0fd3fefa-4f50-4e1b-b32b-b947cdcfee28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.93      0.87      2308\n",
      "           1       0.56      0.27      0.37       692\n",
      "\n",
      "    accuracy                           0.78      3000\n",
      "   macro avg       0.68      0.60      0.62      3000\n",
      "weighted avg       0.75      0.78      0.75      3000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 逻辑回归\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression(max_iter=1000, random_state=42)\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5f8c723c-c3f2-4f80-abb9-361c493a9428",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.98      0.98      0.98      2308\n",
      "           1       0.94      0.93      0.94       692\n",
      "\n",
      "    accuracy                           0.97      3000\n",
      "   macro avg       0.96      0.96      0.96      3000\n",
      "weighted avg       0.97      0.97      0.97      3000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 支撑向量机\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "model = SVC(random_state=42)\n",
    "model.fit(X_train_scaled, y_train)\n",
    "y_pred = model.predict(X_test_scaled)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07b2a125-b490-4795-b57e-d1efc46b6a4f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
